/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.tattoo.workflow.app;

import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.tattoo.workflow.api.dto.*;
import org.tattoo.workflow.domain.bo.Approve;
import org.tattoo.workflow.domain.bo.ProcurementOrderBo;
import org.tattoo.workflow.domain.bo.Review;
import org.tattoo.workflow.domain.bo.Submit;
import org.tattoo.workflow.domain.consts.QueryProcurementMode;
import org.tattoo.workflow.domain.po.ProcessRecord;
import org.tattoo.workflow.domain.po.ProcurementItem;
import org.tattoo.workflow.domain.po.ProcurementOrder;
import org.tattoo.workflow.domain.service.ProcurementService;

import javax.annotation.Resource;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@Service
public class ProcurementAppService {

    @Resource
    private ProcurementService procurementService;

    /**
     * 查询采购订单基本信息
     *
     * @param id 采购订单id
     * @return 基本信息
     */
    public ProcurementOrderResp findProcurementOrder(Long id) {
        ProcurementOrderBo procurementOrder = procurementService.findProcurementOrder(id);
        ProcurementOrderResp procurementOrderResp = new ProcurementOrderResp();
        BeanUtils.copyProperties(procurementOrder, procurementOrderResp);
        return procurementOrderResp;
    }

    /**
     * 查询采购订单明细
     *
     * @param procurementOrderId 采购订单id
     * @return 采购订单明细
     */
    public List<ProcurementItemResp> queryProcurementItems(Long procurementOrderId) {
        List<ProcurementItem> procurementItems = procurementService.queryProcurementItems(procurementOrderId);
        List<ProcurementItemResp> result = new ArrayList<>();
        procurementItems.forEach(pim -> {
            ProcurementItemResp procurementItemResp = new ProcurementItemResp();
            BeanUtils.copyProperties(pim, procurementItemResp);
            result.add(procurementItemResp);
        });
        return result;
    }

    /**
     * 保存采购订单
     * 当前端有id传入，为修改操作，否则为新增操作
     *
     * @param saveProcurementOrderReq 采购订单
     * @return 采购订单id
     */
    public Long saveProcurementOrder(SaveProcurementOrderReq saveProcurementOrderReq) throws Exception {
        ProcurementOrder procurementOrder = new ProcurementOrder();
        BeanUtils.copyProperties(saveProcurementOrderReq, procurementOrder);
        return procurementService.saveProcurementOrder(procurementOrder);
    }

    /**
     * 保存采购订单明细，单条保存
     * 当前端有id传入，为修改操作，否则为新增操作
     *
     * @param saveProcurementItemReq 采购明细
     */
    public void saveProcurementItem(SaveProcurementItemReq saveProcurementItemReq) throws Exception {
        ProcurementItem procurementItem = new ProcurementItem();
        BeanUtils.copyProperties(saveProcurementItemReq, procurementItem);
        procurementService.saveProcurementItem(procurementItem);
    }

    /**
     * 删除采购明细
     *
     * @param id 采购明细id
     */
    public void deleteProcurementItem(Long id) {
        procurementService.deleteProcurementItem(id);
    }

    /**
     * 提交采购订单
     *
     * @param submitReq 采购提交请求
     */
    public void submitProcurementOrder(SubmitReq submitReq) throws Exception {
        Submit submit = new Submit();
        BeanUtils.copyProperties(submitReq, submit);
        procurementService.submitProcurementOrder(submit);
    }

    /**
     * 撤回采购订单
     * 
     * @param id 采购订单id
     * @throws Exception 异常
     */
    public void withdrawProcurementOrder(Long id) throws Exception {
        procurementService.withdrawProcurementOrder(id);
    }

    /**
     * 复合采购订单
     *
     * @param req 复合请求对象
     */
    public void reviewProcurementOrder(ReviewReq req) throws Exception {
        Review review = new Review();
        BeanUtils.copyProperties(req, review);
        procurementService.reviewProcurementOrder(review);
    }

    /**
     * 审批采购订单
     *
     * @param req 审批请求对象
     */
    public void approveProcurementOrder(ApproveReq req) throws Exception {
        Approve approve = new Approve();
        BeanUtils.copyProperties(req, approve);
        procurementService.approveProcurementOrder(approve);
    }

    /**
     * 查询流程记录
     *
     * @param procurementOrderId 采购订单id
     * @return 流程记录
     */
    public List<ProcessRecordResp> queryProcessRecords(Long procurementOrderId) {
        List<ProcessRecord> processRecords = procurementService.queryProcessRecords(procurementOrderId);
        List<ProcessRecordResp> result = Lists.newArrayList();
        processRecords.forEach(pr -> {
            ProcessRecordResp prr = new ProcessRecordResp();
            BeanUtils.copyProperties(pr, prr);
            result.add(prr);
        });
        return result;
    }

    /**
     * 查询采购订单
     *
     * @param queryMode 查询类型
     * @return 采购订单
     */
    public List<ProcurementOrderResp> queryProcurementOrders(QueryProcurementMode queryMode) throws Exception {
        List<ProcurementOrderBo> procurementOrders = procurementService.queryProcurementOrders(queryMode);
        List<ProcurementOrderResp> result = Lists.newArrayList();
        procurementOrders.forEach(po -> {
            ProcurementOrderResp por = new ProcurementOrderResp();
            BeanUtils.copyProperties(po, por);
            result.add(por);
        });
        return result;
    }

    /**
     * 导出采购订单
     *
     * @return 采购订单excel数据流
     */
    public OutputStream exportProcurementOrders() throws Exception {
        return procurementService.exportProcurementOrders();
    }
}
